package com.sui.bigdata.flink.sql.function.scalar;

import org.apache.flink.table.functions.ScalarFunction;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author YongChen
 * @date 2018/12/25 10:18
 * @description
 * @email yong_chen@sui.com
 */
public class CurrentTime extends ScalarFunction  {
    public String eval(Long ts) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        return df.format(new Date(ts));
    }
    public String eval() {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        return df.format(new Date(System.currentTimeMillis()));
    }

    public String eval(int n) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cl = Calendar.getInstance();
        int nday = cl.get(5);
        cl.set(5, nday - n);
        return sdf.format(cl.getTime());
    }
}
